//
//  UIButton+ImageTitleLocationSpace.m
//  Airyclub
//
//  Created by 刘博 on 2020/1/3.
//  Copyright © 2020 Lebbay. All rights reserved.
//

#import "UIButton+ImageTitleLocationSpace.h"
#import <objc/runtime.h>

static void *hasSetSpace = &hasSetSpace;

static void *originContentInset = &originContentInset;

@implementation UIButton (ImageTitleLocationSpace)

- (void)intervalSpace:(CGFloat)space
{
    if (self.hasSetPositionAndSpace) {
        return;
    }
    if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
        //反向语言处理
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -space/2, 0, space/2)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, space/2, 0, -space/2)];
    } else {
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, space/2, 0, -space/2)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, -space/2, 0, space/2)];
    }
    [self _updateContentInsetWithSpace:space];
}

- (void)rightImgWithIntervalSpace:(CGFloat)space
{
    if (self.hasSetPositionAndSpace) {
        return;
    }
    CGFloat imageWidth = self.imageView.image.size.width;
    CGFloat titleWidth = self.titleLabel.intrinsicContentSize.width;
    if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
        //反向语言处理
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, -titleWidth - space/2.0, 0, titleWidth + space/2.0)];
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, imageWidth + space/2, 0, -imageWidth-space/2.0)];
    } else {
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, titleWidth + space/2.0, 0, -titleWidth -space/2.0)];
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -imageWidth - space/2, 0, imageWidth + space/2.0)];
    }
    [self _updateContentInsetWithSpace:space];
}

// 修改button 的内容内边距，防止button宽度过小导致内容左右溢出
- (void)_updateContentInsetWithSpace:(CGFloat)space
{
    if (!self.hasSetPositionAndSpace) {
        self.originContentInsets = self.contentEdgeInsets;
    }
    CGFloat top = self.contentEdgeInsets.top;
    CGFloat bottom = self.contentEdgeInsets.bottom;
    CGFloat right = self.contentEdgeInsets.right;
    CGFloat left = self.contentEdgeInsets.left;
    if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
        //反向语言处理
        CGFloat temp = right;
        right = left;
        left = temp;
    }
    [self setContentEdgeInsets:UIEdgeInsetsMake(top, space/2 + left, bottom, space/2 + right)];
    self.hasSetPositionAndSpace = YES;
}

#pragma mark - setter & getter
- (void)setHasSetPositionAndSpace:(BOOL)hasSetPositionAndSpace
{
    if (!hasSetPositionAndSpace) {
        self.contentEdgeInsets = self.originContentInsets;
    }
    NSNumber *number = [NSNumber numberWithBool:hasSetPositionAndSpace];
    objc_setAssociatedObject(self, hasSetSpace, number, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)hasSetPositionAndSpace
{
    NSNumber *number = objc_getAssociatedObject(self, hasSetSpace);
    return number.boolValue;
}

- (void)setOriginContentInsets:(UIEdgeInsets)originContentInsets
{
    NSValue *value = [NSValue valueWithUIEdgeInsets:originContentInsets];
    objc_setAssociatedObject(self, originContentInset, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIEdgeInsets)originContentInsets
{
    NSValue *value = objc_getAssociatedObject(self, originContentInset);
    UIEdgeInsets origincontentInsets;
    [value getValue:&origincontentInsets];
    return origincontentInsets;
}

@end

